# -*- coding: UTF-8 -*-
"""
Author: Sunck
note: 封装市面上主流的请求头,便于请求伪装
Created time: 2021/06/28
"""
import random
import time
import configparser
# from mysql.main import python_sql_mysql
from settings_package.db_function import python_sql_mysql
from logger import LOG

config = configparser.ConfigParser()
config.read('./conf/conf.ini')  # 读取配置文件
assist_db_name = config.get('MySQL', 'assist_db_name')

# 请求头列表
user_agent_pc = [  # 谷歌
    'Mozilla/5.0.html (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.html.2171.71 Safari/537.36',
    'Mozilla/5.0.html (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.html.1271.64 Safari/537.11',
    'Mozilla/5.0.html (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.html.648.133 Safari/534.16',
    # 火狐
    'Mozilla/5.0.html (Windows NT 6.1; WOW64; rv:34.0.html) Gecko/20100101 Firefox/34.0.html',
    'Mozilla/5.0.html (X11; U; Linux x86_64; zh-CN; rv:1.9.2.10) Gecko/20100922 Ubuntu/10.10 (maverick) Firefox/3.6.10',
    # opera
    'Mozilla/5.0.html (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.html.2171.95 Safari/537.36 OPR/26.0.html.1656.60',
    # qq浏览器
    'Mozilla/5.0.html (compatible; MSIE 9.0.html; Windows NT 6.1; WOW64; Trident/5.0.html; SLCC2; .NET CLR 2.0.html.50727; .NET CLR 3.5.30729; .NET CLR 3.0.html.30729; Media Center PC 6.0.html; .NET4.0C; .NET4.0E; QQBrowser/7.0.html.3698.400)',
    # 搜狗浏览器
    'Mozilla/5.0.html (Windows NT 5.1) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.html.963.84 Safari/535.11 SE 2.X MetaSr 1.0.html',
    # 360浏览器
    'Mozilla/5.0.html (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.html.1599.101 Safari/537.36',
    'Mozilla/5.0.html (Windows NT 6.1; WOW64; Trident/7.0.html; rv:11.0.html) like Gecko',
    # uc浏览器
    'Mozilla/5.0.html (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.html.2125.122 UBrowser/4.0.html.3214.0.html Safari/537.36',
    "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_8; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50",
    "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50",
    "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0",
    "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; .NET4.0C; .NET4.0E; .NET CLR 2.0.50727; .NET CLR 3.0.30729; .NET CLR 3.5.30729; InfoPath.3; rv:11.0) like Gecko",
    "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)",
    "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0)",
    "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)",
    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)",
    "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:2.0.1) Gecko/20100101 Firefox/4.0.1",
    "Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1",
    "Opera/9.80 (Macintosh; Intel Mac OS X 10.6.8; U; en) Presto/2.8.131 Version/11.11",
    "Opera/9.80 (Windows NT 6.1; U; en) Presto/2.8.131 Version/11.11",
    "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_0) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11",
    "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Maxthon 2.0)",
    "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; TencentTraveler 4.0)",
    "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)",
    "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; The World)",
    "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; SE 2.X MetaSr 1.0; SE 2.X MetaSr 1.0; .NET CLR 2.0.50727; SE 2.X MetaSr 1.0)",
    "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; 360SE)",
    "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Avant Browser)",
    "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)"]


def get_user_agent_pc():
    """
    随机返回一个请求头数据
    :return: 一个完整的请求头数据
    """
    return {'user-agent': random.choice(user_agent_pc),
            # 下列cookie在请求遇到封禁的时候可以使用,正常情况下不需要使用到
            # 'Cookie':'__jdv=76161171|direct|-|none|-|1622011788556; shshshfpb=sNkTy%2FTMexuUFgEg3qDBnog%3D%3D; shshshfpa=7fffdeb7-cf79-59dd-67c8-ab8624ebec1b-1620280517; _pst=jd_4d67a4592377b; unick=jd_136979ujt; pin=jd_4d67a4592377b; _tp=e0sgSBYaRpcpNV3dAX6QEz1%2BCa%2FZZ5OTqLkrxAzsN8A%3D; pinId=Q-wxhgRRXkB4mSiLwi2ENbV9-x-f3wj7; user-key=2a5db057-ec9a-4451-8565-6f9e3d7712e6; __jdu=16220117885551004913237; areaId=3; ipLoc-djd=3-51035-39620-0; __jda=122270672.16220117885551004913237.1622011789.1622541600.1622549322.49; __jdc=122270672; shshshfp=1306a6a18a815242e5ef63b3a76badc5; wlfstk_smdl=eurtameqbgh3de6ytcjwwrsg6wdxy6mc; logintype=wx; npin=jd_4d67a4592377b; 3AB9D23F7A4B3C9B=5TX52GHMM2E5BEBMZUQHYZ3KQAKBVGNG2G2V4GH2RR3EY2EFNAGIHSNTHEYZSLYGZK3IPNZLKAUGKIXXOEJNFL6UTI; shshshsID=fe85338bfe85778a6ff342ce727ce105_6_1622549626172; __jdb=122270672.14.16220117885551004913237|49.1622549322; thor=6EE7813FFEF8CC9A9434FC31BBE87F086814D612B58C9C6A20A60F783FB7714E6F792C506E86C491971465BDA18DEEEEA46CDE57DBA8123D0731A63E314B3F365688DF95C0802354F979ABA7AD078DFDECCAAE4D6CB408293D090FA9CEB61D72962A60CE5C43FE2C6FE0116AB99DF4C874EC6EBA95D1737B66CFB42B9E86506AC5616088E1A01277F93AF43B682198FBC9E11C28E9DAE6D6713CA193A0EFE5EB',
            # 'Cookie':'__jdu=56171293; shshshfpa=7fffdeb7-cf79-59dd-67c8-ab8624ebec1b-1620280517; shshshfpb=sNkTy%2FTMexuUFgEg3qDBnog%3D%3D; user-key=66be2699-cfaa-4664-8e65-f2f644f2891f; qrsc=3; rkv=1.0; pinId=Q-wxhgRRXkB4mSiLwi2ENbV9-x-f3wj7; pin=jd_4d67a4592377b; unick=jd_136979ujt; ceshi3.com=000; _tp=e0sgSBYaRpcpNV3dAX6QEz1%2BCa%2FZZ5OTqLkrxAzsN8A%3D; _pst=jd_4d67a4592377b; areaId=6; ipLoc-djd=6-303-304-0; unpl=V2_ZzNtbUBURRIhW09XeB5eVmJTG19KB0cdcQpBUn4fCQBuAREPclRCFnUUR1BnGV8UZwYZXkFcQhRFCEdkeBBVAWMDE1VGZxBFLV0CFSNGF1wjU00zQwBBQHcJFF0uSgwDYgcaDhFTQEJ2XBVQL0oMDDdRFAhyZ0AVRQhHZH8eXANnABpfcmdEJUU4RVR4H1wGVwIiXHIVF0lyCUBQexARAWADFF1BX0EldDhF; __jdv=76161171|baidu-pinzhuan|t_288551095_baidupinzhuan|cpc|0f3d30c8dba7459bb52f2eb5eba8ac7d_0_3276eb82263b4a839a59536747d4832c|1620800521250; PCSYCityID=CN_330000_330100_0; __jda=122270672.56171293.1620280513.1622002310.1622004380.53; __jdc=122270672; shshshfp=67ba12dfeaf6e7c783839f946ef32a05; wlfstk_smdl=9enhqz7l5fc7xlk951i2ffvol5mievto; logintype=wx; npin=jd_4d67a4592377b; thor=6EE7813FFEF8CC9A9434FC31BBE87F086814D612B58C9C6A20A60F783FB7714E483664C46737E4FA5C1480F246776691E38F9AAC56A65A700C05EB148FA89B3AA3F82405691B558248A097D418D4C804ED36B9D14E9323B19A2444C5154E571979AF0BFFD2FDF7E5B98802E64720D2805C3536A717C8037C1771BF09005688C6C05DFB09057FD013AAA70E75536552D3EFF16CE78733EDAF729E8B1340973FAA; __jdb=122270672.24.56171293|53.1622004380; shshshsID=1c048f3ffc9ce15fbbc58395afdf1140_22_1622007261939; 3AB9D23F7A4B3C9B=5TX52GHMM2E5BEBMZUQHYZ3KQAKBVGNG2G2V4GH2RR3EY2EFNAGIHSNTHEYZSLYGZK3IPNZLKAUGKIXXOEJNFL6UTI'
            # 'Cookie':'thor=6EE7813FFEF8CC9A9434FC31BBE87F086814D612B58C9C6A20A60F783FB7714E73A1B0D7CE745FFF942057AE1D47F5E4485FD767FB520F89144910F677840B1E0B5EA3C82B06A7755DF17762B6B3A93FE1B632C42D3F9BB03E1B43F3572AE2B84130AA0AFBB2EBDD2A5576F24EEBAA80FC3881E5427CA350765B93A886FD165C71BA24B66278CF84253B482947541D9CAFAA0E00B61EF4E5690D5907BF8A2969',
            # 'Cookie':'pinId=Q-wxhgRRXkB4mSiLwi2ENbV9-x-f3wj7; user-key=2a5db057-ec9a-4451-8565-6f9e3d7712e6; __jdu=16220117885551004913237; ipLoc-djd=15-1213-3038-59931; shshshfp=a1063368fcb6cea36dd3df823b3a0842;shshshsID=c4cd2fe6311c7ddb9e80adc7ff3d49e4_3_1622096969770; __jda=122270672.16220117885551004913237.1622011789.1622090306.1622095554.9; __jdb=122270672.9.16220117885551004913237|9.1622095554; __jdc=122270672; 3AB9D23F7A4B3C9B=5TX52GHMM2E5BEBMZUQHYZ3KQAKBVGNG2G2V4GH2RR3EY2EFNAGIHSNTHEYZSLYGZK3IPNZLKAUGKIXXOEJNFL6UTI',
            }


def get_product_html_user_agent_pc():
    """
    随机返回页面请求头数据
    :return: 返回页面请求头数据
    """
    cookie = [  # 无痕浏览
        '__jdv=76161171|direct|-|none|-|1626085226360; __jdu=1626085226359542516541; areaId=15; PCSYCityID=CN_330000_330100_0; shshshfpa=5d7f1c71-9f4a-fca8-715a-94511a1e05c5-1626085227; shshshfpb=ooWpoRy6N1QJpzHxqcOmglw%3D%3D; ipLoc-djd=15-1213-3038-59931; shshshfp=b2316f4ec03b9738e4d363c4834d9021; __jda=122270672.1626085226359542516541.1626085226.1626085226.1626085226.1; __jdc=122270672; __jdb=122270672.17.1626085226359542516541|1.1626085226; shshshsID=2d70de4b19de821fb9c4e31ff3a6add7_17_1626085615884; 3AB9D23F7A4B3C9B=5TX52GHMM2E5BEBMZUQHYZ3KQAKBVGNG2G2V4GH2RR3EY2EFNAGIHSNTHEYZSLYGZK3IPNZLKAUGKIXXOEJNFL6UTI',
        # 无痕浏览2
        'shshshfp=b2316f4ec03b9738e4d363c4834d9021; shshshfpa=aaed7c7d-f43a-c285-0a15-18ba1fed97da-1626087550; shshshsID=2798c338e06571db0ee7a344601b182f_1_1626087550433; __jda=122270672.1626087550525599401440.1626087551.1626087551.1626087551.1; __jdb=122270672.1.1626087550525599401440|1.1626087551; __jdc=122270672; __jdv=122270672|direct|-|none|-|1626087550525; shshshfpb=oxAaiS9qZi3SYzr0%2FflSthA%3D%3D; areaId=15; ipLoc-djd=15-1213-3038-59931; __jdu=1626087550525599401440; 3AB9D23F7A4B3C9B=5TX52GHMM2E5BEBMZUQHYZ3KQAKBVGNG2G2V4GH2RR3EY2EFNAGIHSNTHEYZSLYGZK3IPNZLKAUGKIXXOEJNFL6UTI',
        # 普通浏览器 big
        'shshshfpb=sNkTy%2FTMexuUFgEg3qDBnog%3D%3D; shshshfpa=7fffdeb7-cf79-59dd-67c8-ab8624ebec1b-1620280517; __jdu=16220117885551004913237; unpl=V2_ZzNtbURSERVzC0VRKUpbAmIFEg9LUEZFfVoVUHMcDgdgBkBUclRCFnUUR1NnGVgUZgsZXEVcQhRFCEdkeBBVAWMDE1VGZxBFLV0CFSNGF1wjU00zQwBBQHcJFF0uSgwDYgcaDhFTQEJ2XBVQL0oMDDdRFAhyZ0AVRQhHZHseXwZmAxJbS15HEXEIR1V%2bEVwHZAEUbXJQcyVFAEZdfhldNWYzE20AAx8QdQhHVHlUXAJkABNdQlFKHHEMQlR6GFkNZwERX0RnQiV2; user-key=0f8c222e-e0a3-4356-bd22-78bb7706e8ea; areaId=2; PCSYCityID=CN_330000_330100_0; __jdv=122270672|baidu|-|organic|not set|1625811740752; _pst=jd_UglbPOvyQsNS; unick=jd_UglbPOvyQsNS; pin=jd_UglbPOvyQsNS; _tp=GYTK4UfWKfaRRTtA%2BJawhg%3D%3D; pinId=ompzFfKBjhpCVf5UyD3pdQ; __jdc=122270672; __jda=122270672.16220117885551004913237.1622011789.1626079361.1626087683.221; wlfstk_smdl=vaf1bsenbqlx60wwgnwmyb0o777cp3cl; ipLoc-djd=2-2813-61126-0; shshshfp=b2316f4ec03b9738e4d363c4834d9021; __jdb=122270672.14.16220117885551004913237|221.1626087683; shshshsID=ca91c1535ebd46fab92e2cbca485dce9_12_1626087834478; 3AB9D23F7A4B3C9B=5TX52GHMM2E5BEBMZUQHYZ3KQAKBVGNG2G2V4GH2RR3EY2EFNAGIHSNTHEYZSLYGZK3IPNZLKAUGKIXXOEJNFL6UTI',
        # 普通wang deng big
        'shshshfpb=sNkTy%2FTMexuUFgEg3qDBnog%3D%3D; shshshfpa=7fffdeb7-cf79-59dd-67c8-ab8624ebec1b-1620280517; __jdu=16220117885551004913237; unpl=V2_ZzNtbURSERVzC0VRKUpbAmIFEg9LUEZFfVoVUHMcDgdgBkBUclRCFnUUR1NnGVgUZgsZXEVcQhRFCEdkeBBVAWMDE1VGZxBFLV0CFSNGF1wjU00zQwBBQHcJFF0uSgwDYgcaDhFTQEJ2XBVQL0oMDDdRFAhyZ0AVRQhHZHseXwZmAxJbS15HEXEIR1V%2bEVwHZAEUbXJQcyVFAEZdfhldNWYzE20AAx8QdQhHVHlUXAJkABNdQlFKHHEMQlR6GFkNZwERX0RnQiV2; user-key=0f8c222e-e0a3-4356-bd22-78bb7706e8ea; areaId=2; PCSYCityID=CN_330000_330100_0; __jdv=122270672|baidu|-|organic|not set|1625811740752; ipLoc-djd=2-2813-61122-0; _pst=jd_UglbPOvyQsNS; unick=jd_UglbPOvyQsNS; pin=jd_UglbPOvyQsNS; _tp=GYTK4UfWKfaRRTtA%2BJawhg%3D%3D; pinId=ompzFfKBjhpCVf5UyD3pdQ; __jdc=122270672; __jda=122270672.16220117885551004913237.1622011789.1626079361.1626087683.221; thor=467EC920F9099F8A3CA0A60721C3404AF8AC7491BCCE8F753EE063E0077723ADA7C8660BF6C36B3D01719FBF1858B908D7DA4191A9D15432E2FA8FF9197F410E25BE873BC5019950769B842F518DBC779C7C70A452DDE5770BF01DD61489F2B7499D1CE677653FA081F64F81028A0F75CB2BED52727D2818E2F2242DEABD7CA502BEA2D4E2A2B553566CE0FCC09B08BBB80AEEE976DC81A77D83952C69919322; shshshfp=b2316f4ec03b9738e4d363c4834d9021; shshshsID=ca91c1535ebd46fab92e2cbca485dce9_4_1626087705621; __jdb=122270672.4.16220117885551004913237|221.1626087683; 3AB9D23F7A4B3C9B=5TX52GHMM2E5BEBMZUQHYZ3KQAKBVGNG2G2V4GH2RR3EY2EFNAGIHSNTHEYZSLYGZK3IPNZLKAUGKIXXOEJNFL6UTI',
        # 普通 hong dian
        '__jdu=2037093595; shshshfpa=d329595e-caad-e253-239d-59b7745d7d5f-1597050594; shshshfpb=vA3z1jOADTTsIo2NfTPCU%2Fw%3D%3D; pin=jd_4d67a4592377b; unick=jd_136979ujt; _tp=e0sgSBYaRpcpNV3dAX6QEz1%2BCa%2FZZ5OTqLkrxAzsN8A%3D; _pst=jd_4d67a4592377b; areaId=15; ipLoc-djd=15-1213-1214-52672; mt_xid=V2_52007VwATUFtZWlgbQR1sBjICRQdaWVNGFksQWhliBBsFQQtRDx9VSQ8CMwVFVA5eAFpPeRpdBmcfE1VBWlNLH04SWAZsBhRiXWhSahpKHVUNYQEVVV9oV1kZSg%3D%3D; __jdv=122270672|direct|-|none|-|1625374259196; PCSYCityID=CN_330000_330100_330110; pinId=Q-wxhgRRXkB4mSiLwi2ENbV9-x-f3wj7; __jda=122270672.2037093595.1597050592.1626055391.1626087897.75; __jdc=122270672; shshshfp=632078cf6ae07b2a3ab44a3cfe7c51bf; wlfstk_smdl=v9xbk86o5uzgg63f1dlc882ckbt04l1z; logintype=wx; npin=jd_4d67a4592377b; thor=6EE7813FFEF8CC9A9434FC31BBE87F086814D612B58C9C6A20A60F783FB7714EF571C1B5CABBA85AC0DD0A4154B313A7B852980D39F53C8E95763920B22FAB9BAB58B65C768FB1AA8FD399052D832DB03F6FB3FFBB1D5037F7FD3380EA0F951B74E5525CB888055CE6D1B69AC87FCDE9A54D0CF2F2F942D52829C3D611A0C6D1DBBCA1FBDCDA52FE84F2C2E25CFE31A5B19F50F14887CF9DD19B071C27BE42DD; shshshsID=b5f93ef88c9f7735f08bf90c7e7024f9_5_1626087972712; __jdb=122270672.7.2037093595|75.1626087897; 3AB9D23F7A4B3C9B=MBJKNKEVTCNOYZHREDJ2B64KSHZGNSURLA2LARSYF3WJUCJYIAND3MNOW6XDGLJN47USZP62IAJR6DUQTOF53W52BM',
        # 普通 hong dian  hong deng
        '__jdu=2037093595; shshshfpa=d329595e-caad-e253-239d-59b7745d7d5f-1597050594; shshshfpb=vA3z1jOADTTsIo2NfTPCU%2Fw%3D%3D; pin=jd_4d67a4592377b; unick=jd_136979ujt; _tp=e0sgSBYaRpcpNV3dAX6QEz1%2BCa%2FZZ5OTqLkrxAzsN8A%3D; _pst=jd_4d67a4592377b; areaId=15; ipLoc-djd=15-1213-1214-52672; mt_xid=V2_52007VwATUFtZWlgbQR1sBjICRQdaWVNGFksQWhliBBsFQQtRDx9VSQ8CMwVFVA5eAFpPeRpdBmcfE1VBWlNLH04SWAZsBhRiXWhSahpKHVUNYQEVVV9oV1kZSg%3D%3D; __jdv=122270672|direct|-|none|-|1625374259196; PCSYCityID=CN_330000_330100_330110; pinId=Q-wxhgRRXkB4mSiLwi2ENbV9-x-f3wj7; __jda=122270672.2037093595.1597050592.1626055391.1626087897.75; __jdc=122270672; shshshfp=632078cf6ae07b2a3ab44a3cfe7c51bf; wlfstk_smdl=v9xbk86o5uzgg63f1dlc882ckbt04l1z; logintype=wx; npin=jd_4d67a4592377b; thor=6EE7813FFEF8CC9A9434FC31BBE87F086814D612B58C9C6A20A60F783FB7714EF571C1B5CABBA85AC0DD0A4154B313A7B852980D39F53C8E95763920B22FAB9BAB58B65C768FB1AA8FD399052D832DB03F6FB3FFBB1D5037F7FD3380EA0F951B74E5525CB888055CE6D1B69AC87FCDE9A54D0CF2F2F942D52829C3D611A0C6D1DBBCA1FBDCDA52FE84F2C2E25CFE31A5B19F50F14887CF9DD19B071C27BE42DD; shshshsID=b5f93ef88c9f7735f08bf90c7e7024f9_5_1626087972712; __jdb=122270672.7.2037093595|75.1626087897; 3'
        # 普通 hong dian wang deng 
        '__jdu=2037093595; shshshfpa=d329595e-caad-e253-239d-59b7745d7d5f-1597050594; shshshfpb=vA3z1jOADTTsIo2NfTPCU%2Fw%3D%3D; areaId=15; ipLoc-djd=15-1213-1214-52672; __jdv=122270672|direct|-|none|-|1625374259196; PCSYCityID=CN_330000_330100_330110; mt_xid=V2_52007VwATUFtZWlgbQR1sUTQHF1YJWVtGTE4eWxliBUUCQVAHXR5VEQ5WMAdGVFhfUA5PeRpdBmcfE1RBWFFLH0ESXQ1sAxNiX2hSahxAH1QMZwMXUl9oUF4eTA%3D%3D; shshshfp=632078cf6ae07b2a3ab44a3cfe7c51bf; __jdc=122270672; __jda=122270672.2037093595.1597050592.1626055391.1626087897.75; wlfstk_smdl=l1liaj3vg6n6rt04rbmujfkcdxuveln0; _pst=jd_UglbPOvyQsNS; logintype=wx; unick=jd_UglbPOvyQsNS; pin=jd_UglbPOvyQsNS; npin=jd_UglbPOvyQsNS; thor=467EC920F9099F8A3CA0A60721C3404AF8AC7491BCCE8F753EE063E0077723AD6BAB1024FDED4A3604090D75B5DF9480BCBD089331B582146FBE728AFB28D627839BB1DE169893F84E684FD85D35A01D4D4796ECA4AF1C6E281225142586FA8B53848A9884D6062DD081A653B5F8B1E9AED55157408EC8D40193339E64BBC6C4D3C85BC70055B578C999CE791D551AE1A1FCD4A374D04D808C239F75414D633B; _tp=GYTK4UfWKfaRRTtA%2BJawhg%3D%3D; pinId=ompzFfKBjhpCVf5UyD3pdQ; shshshsID=b5f93ef88c9f7735f08bf90c7e7024f9_9_1626088232108; __jdb=122270672.13.2037093595|75.1626087897; 3AB9D23F7A4B3C9B=MBJKNKEVTCNOYZHREDJ2B64KSHZGNSURLA2LARSYF3WJUCJYIAND3MNOW6XDGLJN47USZP62IAJR6DUQTOF53W52BM',
        # 普通 cong dian
        '__jdv=76161171|direct|-|none|-|1625909290079; __jdu=16259092900781375616436; areaId=21; ipLoc-djd=21-1832-3508-0; PCSYCityID=CN_360000_360200_360203; shshshfpa=1b2c708b-bdc3-e03e-647f-09ef63542428-1625909291; shshshfpb=eYamxJSZw%20rwbAVcQc5AK8g%3D%3D; shshshfp=4149b44d96c2f5b17c88dd2e42090703; __jda=122270672.16259092900781375616436.1625909290.1626058629.1626088576.3; __jdc=122270672; __jdb=122270672.3.16259092900781375616436|3.1626088576; shshshsID=da520ebff57f468a083609012d5b52d4_3_1626088587355; 3AB9D23F7A4B3C9B=3DWOY62XLU75XUCLUKJAP25K7JKWJLWS3UHTPVGROAON3EUNGUIVORZOTRDDPTYTX4ORID3A6D445TXD7UEWXELPPE',
        # 普通 cong dian hong deng
        '__jdv=76161171|direct|-|none|-|1625909290079; __jdu=16259092900781375616436; areaId=21; ipLoc-djd=21-1832-3508-0; PCSYCityID=CN_360000_360200_360203; shshshfpa=1b2c708b-bdc3-e03e-647f-09ef63542428-1625909291; shshshfpb=eYamxJSZw%20rwbAVcQc5AK8g%3D%3D; thor=6EE7813FFEF8CC9A9434FC31BBE87F086814D612B58C9C6A20A60F783FB7714E0128FCB52A5F2562CE28F4ADA37ADECDC4AAD4D7818D6E6F46A3920DC8BC3B3B2CFBAC6A6ECBA915EDDBB8DEC8AA6F535CE05C21F1DF16744A9CA7A8BFD66D5773E559696C40BF4953F9601BE9341BB445335ED8CF14AAC4637EDDA5F836FEA61BB5E700C109D0FF6A84838B45472D968E71EE0BB7D6691546424C9FE675A247; pinId=Q-wxhgRRXkB4mSiLwi2ENbV9-x-f3wj7; pin=jd_4d67a4592377b; unick=jd_136979ujt; ceshi3.com=000; _tp=e0sgSBYaRpcpNV3dAX6QEz1%2BCa%2FZZ5OTqLkrxAzsN8A%3D; _pst=jd_4d67a4592377b; __jda=122270672.16259092900781375616436.1625909290.1626058629.1626088576.3; __jdc=122270672; shshshfp=4149b44d96c2f5b17c88dd2e42090703; __jdb=122270672.11.16259092900781375616436|3.1626088576; shshshsID=da520ebff57f468a083609012d5b52d4_9_1626088692012; 3AB9D23F7A4B3C9B=3DWOY62XLU75XUCLUKJAP25K7JKWJLWS3UHTPVGROAON3EUNGUIVORZOTRDDPTYTX4ORID3A6D445TXD7UEWXELPPE', ]
    return {'user-agent': random.choice(user_agent_pc),
            'referer': 'https://search.jd.com/',
            'cookie': random.choice(cookie)}


def get_comment_ua(db_name=assist_db_name):
    """
    将存储在mysql的cookie数据进行组转
    :param db_name: cookie存储的数据库名字
    :return: 组装好的cookie字典和可选cookie列表
    """
    while True:
        select_sql = "select purpose_value from request_header where " \
                     "purpose_key='comment_condition'"
        is_return = python_sql_mysql(sql=select_sql, is_return=True,
                                     db_name=db_name)
        if is_return:
            if is_return[0][0] == '1':
                break
            else:
                time.sleep(10)
    db_name = db_name
    select_sql = "select purpose_value from request_header where " \
                 "purpose_key='comment_cookie'"
    comment_ua_id = \
        python_sql_mysql(sql=select_sql, db_name=db_name, is_return=True)[0]
    comment_ua_id_list = [int(i) for i in comment_ua_id[0].split(',')]
    select_sql = "select id,purpose_value from request_header " \
                 "where purpose_key='get_json_user_agent_pc'"
    comment_ua = python_sql_mysql(sql=select_sql, db_name=db_name,
                                  is_return=True)
    comment_ua_dict = {id: ua for id, ua in comment_ua}
    return comment_ua_id_list, comment_ua_dict


def get_json_user_agent_pc():
    """
    随机返回请求json数据的请求头
    :return: 返回请求json数据的请求头
    """
    comment_ua_id_list, comment_ua_dict = get_comment_ua()
    num = random.choice(comment_ua_id_list)
    LOG.info('%s' % (num))
    if num == 1:
        json_user_agent = {'user-agent': random.choice(user_agent_pc),
                           'referer': 'https://item.jd.com/'}
    else:
        json_user_agent = {'user-agent': random.choice(user_agent_pc),
                           'referer': 'https://item.jd.com/',
                           'Cookie': comment_ua_dict.get(num)}
    return json_user_agent


if __name__ == '__main__':
    json = get_json_user_agent_pc()
    print(json)
